package com.gameley.dmgtpf.root.controller;

import com.gameley.dmgtpf.common.core.aspect.MyDoneTime;
import com.gameley.dmgtpf.common.utils.PubEnvUtil;
import com.gameley.dmgtpf.root.entity.AdminsEntity;
import com.gameley.dmgtpf.root.service.AdminsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

/**
 * @author libencheng
 * @email libencheng@gmail.com
 * @date 2021-07-07 14:57:36
 */
@Controller
@RequestMapping("root/admins")
public class AdminsController
{
    @Autowired
    private AdminsService adminsService;
    
    /**
     * 列表
     */
    @MyDoneTime(param = "AdminsController.rootlist")
    @RequestMapping("/rootlist")
    @RequiresPermissions("platform:user:list")
    public String list(Model model, HttpSession session)
    {
        
        if (PubEnvUtil.isEmptyObj(session.getAttribute("loginUser")))
        {
            return "index.html";
        }
        
        List<AdminsEntity> admins = adminsService.selectObjs();
        PubEnvUtil.setModel(model, admins);
        
        return "root/rootlist";
    }
    
    @MyDoneTime(param = "AdminsController.addPage")
    @RequestMapping("/add")// 去增加员工界面
    public String addPage(Model model)
    {
        
        return "root/add";
    }
    
    /**
     * 保存
     */
    @MyDoneTime(param = "AdminsController.save")
    @PostMapping(value = "/save")
    @RequiresPermissions("root:admins:save")
    public String save(Model model, HttpSession session, AdminsEntity admin) throws Exception
    {
        PubEnvUtil.setAttributeValue(admin);
        String password = PubEnvUtil.isEmptyObj(admin.getPassword()) ? "123456" : admin.getPassword();
        admin.setPassword(admin.getName() + PubEnvUtil.md5(password));
        adminsService.register(admin);
        
        return list(model, session);
    }
    
    @MyDoneTime(param = "AdminsController.resetPassword")
    @RequestMapping("/Reset/{id}")
    @RequiresPermissions("root:admins:Reset")
    public String resetPassword(@PathVariable("id") Integer id, Model model, HttpSession session) throws NoSuchAlgorithmException
    {
        
        AdminsEntity admin = adminsService.getById(id);
        
        if (!PubEnvUtil.isEmptyObj(admin))
        {
            admin.setPassword(admin.getCode() + PubEnvUtil.md5("123456"));
            adminsService.updateById(admin);
        }
        
        return list(model, session);
    }
    
    /**
     * 修改
     *
     * @RequestBody 加到参数时，会以字符串形式传参 传对象会报一下错误，这里不加 There was an unexpected error (type=Unsupported Media Type,
     *              status=415). Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
     */
    @MyDoneTime(param = "AdminsController.update")
    @RequestMapping("/update")
    @RequiresPermissions("root:admins:update")
    public String update(Model model, HttpSession session, AdminsEntity entity) throws NoSuchAlgorithmException
    {
        PubEnvUtil.setUpdateAttrValue(entity);
        
        if (!PubEnvUtil.isEmptyObj(entity.getPassword()))
        {
            String pwd = entity.getCode() + PubEnvUtil.md5(entity.getPassword());
            entity.setPassword(pwd);
        }
        else
        {
            AdminsEntity byId = adminsService.getById(entity.getId());
            entity.setPassword(byId.getPassword());
        }
        adminsService.updateById(entity);
        
        return list(model, session);
    }
    
    // 去修该员工界面
    @MyDoneTime(param = "AdminsController.updatePage")
    @GetMapping("/update/{id}")
    public String updatePage(@PathVariable("id") Integer id, Model model)
    {
        AdminsEntity admin = adminsService.getById(id);
        model.addAttribute("temp", admin);
        return "root/update";
    }
    
    /**
     * 删除
     */
    @MyDoneTime(param = "AdminsController.delete")
    @RequestMapping("/delete")
    @RequiresPermissions("root:admins:delete")
    public String delete(@RequestBody Integer[] ids)
    {
        adminsService.removeByIds(Arrays.asList(ids));
        
        return "root/rootlist";
    }
    
    // 删除员工
    @MyDoneTime(param = "AdminsController.deleteMethod")
    @GetMapping("/delete/{id}")
    public String deleteMethod(Model model, @PathVariable("id") Integer id)
    {
        adminsService.removeByIds(Arrays.asList(id));
        List<AdminsEntity> admins = adminsService.selectObjs();
        model.addAttribute("temps", admins);
        
        return "root/rootlist";
    }
}
